﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TCL.FUNC
{
    public class NullEx
    {

        public static object getNullValue(object objField)
        {
            if (objField == null)
                return DBNull.Value;

            if (objField is Int16 || objField is Int32 || objField is Int64 || objField is int)
            {
                if (Convert.ToInt64(objField) == 0)
                {
                    return DBNull.Value;
                }
                else
                {
                    return objField;
                }
            }
            if (objField is Decimal || objField is Double || objField is float)
            {
                if (Convert.ToDecimal(objField) == 0)
                {
                    return DBNull.Value;
                }
                else
                {
                    return objField;
                }
            }
            if (objField is String || objField is string)
            {
                if (String.IsNullOrEmpty(Convert.ToString(objField)))
                {
                    return DBNull.Value;
                }
                else
                {
                    return objField;
                }
            }
            if (objField is DateTime)
            {
                if (Convert.ToDateTime(objField) == DateTime.MinValue || Convert.ToDateTime(objField) == DateTime.MaxValue)
                {
                    return DBNull.Value;
                }
                else
                {
                    return objField;
                }
            }
            if (objField is Boolean || objField is bool)
            {
                return objField;
            }
            return DBNull.Value;
        }

        public static String getNullValueSQLite(String sInput)
        {
            sInput = sInput.Replace("''", "NULL");
            sInput = sInput.Replace("'NULL'", "NULL");
            sInput = sInput.Replace("'1753-01-01 00:00:00.000'", "NULL");
            sInput = sInput.Replace("'0001-01-01 00:00:00.000'", "NULL");
            return sInput;
        }
    }
}
